package ks.app.smartDic;


import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import net.daum.mobilead.AdConfig;
import net.daum.mobilead.AdHttpListener;

import net.daum.mobilead.MobileAdView;

public class MultipleAd {

	static final private int ADMOB = 1;
	static final private int ADAM = 2;

	private String countrySO;
	private MobileAdView adViewAdam;
	private Context context;

	
	static private String adamPublishID = "41eZ0WT12eb93600fb";
	static Boolean DEBUG = false;
	static final int KOREA = ADAM;
	static final int FOREIGN_COUNTRY = ADAM;

	MultipleAd(Context pContext, MobileAdView pAdViewAdam) {

		context = pContext;
		adViewAdam = pAdViewAdam;
		setCountryCode();
		setup();
	}

	MultipleAd(Context pContext,  MobileAdView pAdViewAdam,
			String pAdamPublishID) {

		context = pContext;
		adViewAdam = pAdViewAdam;
		setCountryCode();
		adamPublishID = pAdamPublishID;
		setup();
	}

	MultipleAd(Context pContext, MobileAdView pAdViewAdam,
			Boolean pDebug) {

		context = pContext;
		adViewAdam = pAdViewAdam;
		setCountryCode();
		DEBUG = pDebug;
		setup();
	}

	private void initAdMob() {

		adViewAdam.setVisibility(0);
		adViewAdam.setEnabled(false);

	}

	private void initAdam() {

		if (!DEBUG) {
			AdConfig.setClientId(adamPublishID);

		} else {
			AdConfig.setClientId("TestClientId");
			String[] dev = { AdConfig.TEST_EMULATOR };
			AdConfig.setTestDevice(dev);
		}

	}

	void setup() {

		if (isKorea())
			selectAdToSetup(KOREA);
		else
			selectAdToSetup(FOREIGN_COUNTRY);
	}

	void displayAd() {

		if (isKorea())
			selectAdToDisplay(KOREA);
		else
			selectAdToDisplay(FOREIGN_COUNTRY);
	}

	private void displayAdmob() {
		
	}

	private void displayAdam() {

		adViewAdam.setAdListener(new AdHttpListener() {

			public void failedDownloadAd_AdListener(int in_err, String in_errMsg) {
				if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_INTERNET_PERMISSION_DENIED) {
					Log.d("Activity", " add android.permission.INTENER");
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_HTTPFAILED) {
					Log.d("Activity", " HTTP failed : " + in_errMsg);
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_SDKEXCEPTION) {
					Log.d("Activity", " SDK exception : " + in_errMsg);
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_UNKNOWN_CLIENTID) {
					Log.d("Activity", " NO client ID : " + in_errMsg);
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_INVALID_DEVICE) {
					Log.d("Activity", " invalid device : " + in_errMsg);
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_INVALIDAD) {
					Log.d("Activity", " invalid AD : " + in_errMsg);
				} else if (in_err == AdConfig.AD_DOWNLOAD_ERROR.AD_DOWNLOAD_ERROR_WRONGPARAMETER) {
					Log.d("Activity", " wrong parameter : " + in_errMsg);
				}
				if (adViewAdam == null
						|| (adViewAdam != null && !adViewAdam.hasAd())) {
					adViewAdam.setEnabled(false);
				} else {
					adViewAdam.setEnabled(true);
				}

			}

			public void didDownloadAd_AdListener() {
				// TODO Auto-generated method stub

			}
		});

		adViewAdam.setEnabled(true);
	}

	private Boolean isKorea() {
		if (countrySO.compareTo("kr") == 0)
			return true;
		else
			return false;
	}

	private void setCountryCode() {

		TelephonyManager tm = (TelephonyManager) context
				.getApplicationContext().getSystemService(
						Context.TELEPHONY_SERVICE);

		countrySO = tm.getSimCountryIso();
	}

	void setAdamPublishID(String pAdamPublishID) {
		adamPublishID = pAdamPublishID;
	}

	void selectAdToSetup(int pAd) {

		if (pAd == ADMOB)
			initAdMob();
		else if (pAd == ADAM)
			initAdam();
	}

	void selectAdToDisplay(int pAd) {

		if (pAd == ADMOB)
			displayAdmob();

		else if (pAd == ADAM)
			displayAdam();
	}

}
